class StoresController < ApplicationController
  before_action :admin_required
  layout 'admin_application'
  before_action :get_store, only: [:show]
  def new
    @store = Store.new(store_basic_info: StoreBasicInfo.new, admin: StoreStaff.new)
  end

  def index
    @stores = Store.includes(:admin).all
  end

  def create
    # @status = CreateStoreService.call(store_params)
    @store = Store.new(store_params)
    if @store.save
      @store.admin.update!(store_id: @store.id)
      redirect_to stores_path, notice: "Store info was successful!"
    else
      redirect_to new_store_path, notice: @store.errors.messages.first
    end
  end

  def show

  end

  private
  def store_params
    params[:store][:admin_attributes][:admin] = true
    params[:store][:admin_attributes][:full_name] = params[:store][:store_basic_info_attributes][:legal_person]
    params.require(:store).permit(
      :name,
      store_basic_info_attributes: [
        :legal_person,
        :address,
        :register_money,
        :company_type,
        :set_from,
        :main_industry
      ],
      admin_attributes: [
        :login_name,
        :phone_number,
        :password,
        :full_name,
        :admin
      ]
    )
  end

  def get_store
    @store = Store.find(params[:id])
  end
end
